<template>
  <div class="count-card">
    <div class="header">
      <span>{{ countCard.title }}</span>
      <el-tooltip class="box-item" effect="light" :content="countCard.tips" placement="top">
        <el-icon><Warning /></el-icon>
      </el-tooltip>
    </div>

    <div class="content">
      <span class="count" ref="countUpRef1">{{ countCard.number1 }}</span>
    </div>

    <div class="footer">
      <span>{{ countCard.subtitle }}</span>
      <span ref="countUpRef2">{{ countCard.number2 }}</span>
    </div>
  </div>
</template>

<script setup lang="ts">
  import type { ICountCardData } from '@/services/main/analysis'
  import { CountUp } from 'countup.js'
  import { onMounted, ref } from 'vue'

  interface IProp {
    countCard: ICountCardData
  }

  const $props = withDefaults(defineProps<IProp>(), {
    countCard: () => ({
      title: '商品总销量',
      number1: 0,
      tips: '',
      number2: 0,
      subtitle: '',
      amount: ''
    })
  })

  // 创建CountUp实例
  const countUpRef1 = ref<HTMLElement>()
  const countUpRef2 = ref<HTMLElement>()
  // https://panjiachen.github.io/countTo/demo/ options配置
  const countOption = {
    startVal: 0,
    // decimals: '3',
    // duration: 100,
    prefix: $props.countCard.amount === 'saleroom' ? '$' : ''
  }
  onMounted(() => {
    // 参数1：需要执行的DOM对象， 参数2：递增的最终值, 参数3：其他options配置
    const countUp1 = new CountUp(countUpRef1.value!, $props.countCard.number1 as number, countOption)
    countUp1.start()

    const countUp2 = new CountUp(countUpRef2.value!, $props.countCard.number2 as number, countOption)
    countUp2.start()
  })
</script>

<style lang="scss" scoped>
  .count-card {
    display: flex;
    flex-direction: column;
    padding: 0 20px;
    height: 130px;
    background: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    .header {
      display: flex;
      height: 36px;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.45);
      justify-content: space-between;
      align-items: flex-end;
    }

    .content {
      flex: 1;
      display: flex;
      margin-left: 0px;
      font-size: 26px;
      color: rgba(0, 0, 0, 0.85);
      flex-direction: column;
      justify-content: center;
      align-items: flex-start;
    }

    .footer {
      display: flex;
      height: 38px;
      line-height: 38px;
      font-size: 14px;
      letter-spacing: 1px;
      color: rgba(0, 0, 0, 0.85);
      border-top: 1px solid #f0f0f0;
    }
  }
</style>
